﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MVVm.Comman
{
    class DelegateCommand : ICommand
    {
        public event EventHandler? CanExecuteChanged;

        /// <summary>
        /// 是否执行命令
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool CanExecute(object? parameter)
        {
            if (this.CanExecuteFunc == null) 
            {
                return true;
            }

            this.CanExecuteFunc(parameter);
            return true;
           
        }

        /// <summary>
        /// 执行命令的内容
        /// </summary>
        /// <param name="parameter"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Execute(object? parameter)
        {
            if (CanExecuteChanged == null)
            {
                return;
            }
            this.ExecuteAction(parameter);
            
        }

        public Action <object> ?ExecuteAction { get; set; }
        public Func <object, bool> ?CanExecuteFunc { get; set; }
    }
}
